﻿using System;

using FreeSql;

using TomNet.Entity;
using TomNet.Exceptions;


namespace TomNet.FreeSql
{
    public class DbHelper
    {
        /// <summary>
        /// 创建数据库
        /// </summary>
        public static void CreateDatabase(DataType type, string conn, string sql)
        {
            if (type == DataType.Sqlite)
            {
                return;
            }

            var db = new FreeSqlBuilder()
                    .UseConnectionString(type, conn)
                    .Build();

            try
            {
                Console.WriteLine("\r\ncreate database started");
                db.Ado.ExecuteNonQuery(sql);
                Console.WriteLine("create database succeed\r\n");
            }
            catch (Exception e)
            {
                Console.WriteLine($"create database failed.\n{e.Message}\r\n");
            }
        }


        public static DataType DatabaseTypeToDbType(DatabaseType dbtype)
        {
            return dbtype switch
            {
                DatabaseType.SqlServer => DataType.SqlServer,
                DatabaseType.Sqlite => DataType.Sqlite,
                DatabaseType.MySql => DataType.MySql,
                DatabaseType.Oracle => DataType.Oracle,
                DatabaseType.PostgreSql => DataType.PostgreSQL,
                _ => throw new TomNetException($"无法识别DataType类型{dbtype}")
            };
        }
    }
}
